package com.fruit.controller.app.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.constants.GoodsContant;
import com.fruit.controller.base.BaseController;
import com.fruit.pojo.common.Page;
import com.fruit.pojo.goods.Base;
import com.fruit.pojo.goods.GoodsSpec;
import com.fruit.pojo.goods.InterfaceConfig;
import com.fruit.pojo.goods.RightConfig;
import com.fruit.pojo.recommend.Recommend;
import com.fruit.service.goods.GoodsManager;
import com.fruit.service.recommend.RecommendManager;
import com.fruit.util.DateUtil;
import com.fruit.util.ResultInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @ClassName: GoodsController
 * @author 葛欢欢
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/app/goods/")
public class GoodsController extends BaseController {

	@Autowired
	private GoodsManager goodsManager;
	
	@Autowired
	private RecommendManager recommendManager;
	
	@Value("${domain}")
    private String domain;
	
	@Value("${h5_domain}")
    private String h5_domain;

	/**
	 * 认养首页
	 * 
	 * @param request
	 *            request
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "/index", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> index(HttpServletRequest request, HttpSession session) {
		long a = System.currentTimeMillis();
		log.info("【获取认养首页进入时间】" + DateUtil.getTime());
		log.info("【获取认养首页】---【开始】---");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> pam = new HashMap<String, Object>();
		pam.put("position", GoodsContant.SEASONAL); // 认养首页列表推荐
		pam.put("start", 0);
		pam.put("num", 6);
		try {
			// 获取商品列表
			List<GoodsSpec> goodsList = goodsManager.getGoodsList(pam); // 获取商品列表
			resultMap.put("goodsList", goodsList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【获取认养首页】---【结束】---");
		log.info("【获取认养首页耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resultMap;
	}

	/**
	 * 商品列表
	 * 
	 * @param page
	 *            page
	 * @param request
	 *            request
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "/getGoods", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getGoods(String classId, Page page, HttpServletRequest request, HttpSession session) {
		long a = System.currentTimeMillis();
		log.info("【获取商品首页加载更多进入时间】" + DateUtil.getTime());
		log.info("【获取商品首页加载更多】---【开始】---");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> pam = new HashMap<String, Object>();
		Map<String, Object> recommendMap = new HashMap<String, Object>();
		String classString = null;
		if (StringUtils.isNotBlank(classId)) {
			classString = goodsManager.getClassIdByPid(classId);
		}
		pam.put("classId", classString); // 总的分类id
		pam.put("position", GoodsContant.SEASONAL); // 认养首页列表推荐
		pam.put("start", (page.getPage() - 1) * page.getShowNum());
		pam.put("num", page.getShowNum());
		try {
			recommendMap.put("classId", classId);
			recommendMap.put("type", "00");//app版本
			//列表推荐位
			List<Recommend> recommendList = recommendManager.findRecommendByPosition(recommendMap);
			// 获取商品列表
			List<GoodsSpec> goodsList = goodsManager.getGoodsList(pam); // 获取商品列表
			resultMap.put("goodsList", goodsList);
			resultMap.put("recommendList", recommendList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【获取商品首页加载更多】---【结束】---");
		log.info("【获取商品首页加载更多耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resultMap;
	}

	/**
	 * 查询基地
	 * 
	 * @param id
	 *            id
	 * @param request
	 *            request
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "/getBase", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getBase(Integer id, HttpServletRequest request, HttpSession session) {
		long a = System.currentTimeMillis();
		log.info("【获取基地信息进入时间】" + DateUtil.getTime());
		log.info("【获取基地信息】---【开始】---");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Base baseInfo = goodsManager.getBase(id); // 获取基地信息
			resultMap.put("BaseInfo", baseInfo);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【获取基地信息】---【结束】---");
		log.info("【获取基地信息耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resultMap;
	}

	/**
	 * 获取首页按钮/个人中心按钮
	 * 
	 * @param appType
	 *            appType
	 * @param request
	 *            request
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "/getInterfaceConfig", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getInterfaceConfig(String appType, HttpServletRequest request, HttpSession session) {
		long a = System.currentTimeMillis();
		log.info("【获取按钮配置进入时间】" + DateUtil.getTime());
		log.info("【获取按钮配置】---【开始】---");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (StringUtils.isEmpty(appType)) {
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "no-param", "缺少参数"));
			return resultMap;
		}
		try {
			// 获取按钮配置
			List<InterfaceConfig> config = goodsManager.getInterfaceConfig(appType);
			if (config != null) {
				for (InterfaceConfig i : config) {
					if("02".equals(i.getInterfaceType())){
						i.setUrl(h5_domain+i.getUrl());
					}
				}
				resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
				resultMap.put("interfaceConfig", config);
			} else {
				resultMap.put("result", this.initResult(ResultInfo.ERROR, "ERROR", ""));
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【获取按钮配置】---【结束】---");
		log.info("【获取按钮配置耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resultMap;
	}

	/**
	 * 首页权益配置
	 * 
	 * @param request
	 *            request
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "/rightConfig", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> rightConfig(HttpServletRequest request, HttpSession session) {
		long a = System.currentTimeMillis();
		log.info("【获取权益配置进入时间】" + DateUtil.getTime());
		log.info("【获取权益配置】---【开始】---");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> recommendMap = new HashMap<String, Object>();
		recommendMap.put("position", "app_index_right_activity");
		try {
			List<RightConfig> rc = goodsManager.getRightConfigList("00");
			if (rc.isEmpty()) {
				log.info("【获取权益配置】---【未查询到数据】---");
				resultMap.put("result", this.initResult(ResultInfo.ERROR, "no", "未查询到数据"));
				return resultMap;
			}
			List<Recommend> recommendList = recommendManager.findRecommendByPosition(recommendMap);
			resultMap.put("rightConfig", rc);
			resultMap.put("recommendList", recommendList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【获取权益配置】---【结束】---");
		log.info("【获取权益配置耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resultMap;
	}

	/**
	 * 认养大厅
	* @author 葛欢欢
	* @version 2017.11.30 16:40 
	* @param request
	* @return
	 */
	@RequestMapping(value = "/getGoodsHall", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getGoodsHall(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			log.info("【进入认养大厅】---【开始】---");
			resultMap = goodsManager.getGoodsHall("00");
			log.info("【进入认养大厅】---【结束】---");
		} catch (Exception e) {
			e.printStackTrace();
			log.info("【进入认养大厅】---【异常】---");
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		return resultMap;

	}
}
